/*
 * @Descripttion: 用户管理功能
 * @Author: YuZhenJing
 * @Date: 2020-01-10 19:55:22
 * @LastEditors: YuZhenJing
 * @LastEditTime: 2020-05-15 09:09:17
 */

/**
 * 查询用户性别数据集
 */
export function searchUserGenderOptions() {
  const options = this.$html.get('user/userGenderOptions')
  options
    .then(data => {
      if (data.returnType == 'success') {
        this.userGenderOptions = this.$objTools.dicToArray(data.returnData)
      }
    })
    .catch(() => {
      console.log('error:searchUserGenderOptions')
    })
}

/**
 * 初始化帐户类别数据集
 */
export function searchAccountTypeOptions() {
  const options = this.$html.get('user/accountTypeOptions')
  options
    .then(data => {
      if (data.returnType == 'success') {
        this.accountTypeOptions = this.$objTools.dicToArray(data.returnData)
      }
    })
    .catch(() => {
      console.log('error:searchAccountTypeOptions')
    })
}

/**
 * 初始化用户分组数据集
 */
export function searchUserGroupOptions() {
  const options = this.$html.get('user/group/list')
  options
    .then(data => {
      if (data.returnType == 'success') {
        this.userGroupList = []
        data.returnData.map(item => {
          this.userGroupList.push({
            value: item.userGroupId,
            name: item.userGroupName
          })
        })
      }
    })
    .catch(() => {
      console.log('error:searchUserGroupOptions')
    })
}

/**
 * 初始化帐户状态数据集
 */
export function searchAccountStatusOptions() {
  const options = this.$html.get('user/accountStatusOptions')
  options
    .then(data => {
      if (data.returnType == 'success') {
        this.accountStatusOptions = this.$objTools.dicToArray(data.returnData)
      }
    })
    .catch(() => {
      console.log('error:searchAccountStatusOptions')
    })
}

/**
 * 查询用户列表信息
 */
export function request() {
  this.tableDataParam.loading = true
  let param = {
    pageSize: this.tableDataParam.paginationData.pageSize,
    pageIndex: this.tableDataParam.paginationData.currentPage,
    filter: this.searchParams,
    sortBy: this.tableDataParam.paginationData.sortItem.prop,
    descending:
      this.tableDataParam.paginationData.sortItem.order == 'descending'
  }
  const options = this.$html.get('user/pagedlist', param)
  options
    .then(res => {
      let data = res.returnData
      this.tableDataParam.data = data.list
      this.tableDataParam.paginationData.total = data.total
      this.tableDataParam.loading = false
    })
    .catch(() => {
      this.tableDataParam.loading = false
      console.log('error:user/request')
    })
}

export function handleBatch(command) {
  if (command === 'start') {
    this.batchStatus(command)
  } else if (command === 'stop') {
    this.batchStatus(command)
  } else if (command === 'del') {
    this.batchDel()
  }
}

/**
 * 删除用户
 */
export function singleDel(rowObj) {
  this.$confirm('确认要删除【' + rowObj.personName + '】吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      this.doDel([rowObj.userId])
    })
    .catch(() => {
      this.$notify.info({
        title: '消息',
        message: '已取消删除'
      })
    })
}

/**
 * 批量更新状态
 */
export function batchStatus(status) {
  const statusName = status === 'start' ? '启用' : '停用'
  let ids = []
  this.tableDataParam.selectedData.map(item => {
    ids.push(item.userId)
  })
  if (ids.length > 0) {
    this.$confirm(
      '您选择了【' + ids.length + '】用户信息，确认要' + statusName + '吗?',
      '提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )
      .then(() => {
        const result = this.$html.putJson('user/updateStatus/' + status, ids)
        result
          .then(data => {
            if (data.returnType == 'success') {
              this.$notify({
                title: '成功',
                message: statusName + '成功',
                type: 'success'
              })
              this.request()
            }
          })
          .catch(() => {
            console.log('error:user/batchStatus')
          })
      })
      .catch(() => {
        this.$notify.info({
          title: '消息',
          message: '已取消状态更新'
        })
      })
  } else {
    this.$alert('请至少选择一条用户信息进行状态更新', '提示', {
      type: 'warning',
      confirmButtonText: '确定'
    })
  }
}

/**
 * 批量删除
 */
export function batchDel() {
  let ids = []
  this.tableDataParam.selectedData.map(item => {
    ids.push(item.userId)
  })
  if (ids.length > 0) {
    this.$confirm(
      '您选择了【' + ids.length + '】用户信息，确认要删除吗?',
      '提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )
      .then(() => {
        this.doDel(ids)
      })
      .catch(() => {
        this.$notify.info({
          title: '消息',
          message: '已取消删除'
        })
      })
  } else {
    this.$alert('请至少选择一条用户信息进行删除', '提示', {
      type: 'warning',
      confirmButtonText: '确定'
    })
  }
}

/**
 * 执行删除
 */
export function doDel(param) {
  const delResult = this.$html.delete('user/delUser', param)
  delResult
    .then(data => {
      if (data.returnType == 'success') {
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success'
        })
        this.request()
      }
    })
    .catch(() => {
      console.log('error:user/doDel')
    })
}

/**
 * 编辑用户信息
 */
export function edit(rowObj) {
  this.windowItem.userEdit.userId = rowObj.userId
  this.windowItem.userEdit.show = true
}

/**
 * 分配用户角色
 */
export function userRole(rowObj) {
  this.$refs.userRoleRef.show(rowObj)
}

/**
 * 重置密码
 */
export function resetPwd(rowObj) {
  this.$confirm(
    '您确认要将【' + rowObj.personName + '】的密码重置吗?',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  )
    .then(() => {
      const result = this.$html.put('user/resetPwd', { userId: rowObj.userId })
      result
        .then(data => {
          if (data.returnType == 'success') {
            this.$notify({
              title: '成功',
              message: '密码重置成功',
              type: 'success'
            })
          }
        })
        .catch(() => {
          console.log('error:user/resetPwd')
        })
    })
    .catch(() => {
      this.$notify.info({
        title: '消息',
        message: '已取消密码重置'
      })
    })
}
